import React from 'react';
import Taro from '@tarojs/taro';
import { connect } from 'react-redux';
import { View } from '@tarojs/components';
import './index.scss';

const getOperatesIndex = (status) => {
  let operates;
  switch (status) {
    case 1:
      operates = [1, 2, 0];
      break;
    case 2:
      operates = [2, 0];
      break;
    case 3:
      operates = [1, 2, 0];
      break;
    case 4:
      operates = [3, 0];
      break;
    case 6:
      operates = [1, 2, 0];
      break;
    case 7:
      operates = [2, 0];
      break;
    case 8:
      operates = [4, 0];
      break;
    default:
      operates = [0];
      break;
  }
  return operates;
};

const BottomOperate = ({ dispatch, detail = {}, handleOp = null }) => {
  let contactAddress = JSON.parse(detail.contactAddress || '{}');
  const handleCallMerchant = () => {
    if (contactAddress?.contact_mobile && contactAddress?.contact_mobile?.length > 0) {
      Taro.makePhoneCall({
        phoneNumber: contactAddress?.contact_mobile || '',
      });
    } else {
      Taro.showToast({
        title: '该商家暂无联系方式',
        duration: 2000,
        icon: 'none',
      });
    }
  };

  const handleModifyApply = () => {
    const { kind } = detail;
    if (kind == 1) {
      //退款
      Taro.navigateTo({
        url: `/orderSubPack/afterSale/applyRefund/index?isModify=true`,
      });
    } else {
      //退货/换货
      Taro.navigateTo({
        url: `/orderSubPack/afterSale/apply/index?isModify=true`,
      });
    }
  };

  const handleCancleApply = () => {
    dispatch({
      type: 'order/applyAfterServiceServer',
      payload: { id: detail.id },
    }).then(() => handleOp());
  };

  const handleLogistic = () => {
    Taro.navigateTo({ url: '/orderSubPack/afterSale/logisticsInput/index' });
  };

  const handleConfirm = () => {
    dispatch({
      type: 'order/confirmGetItem',
      payload: { id: detail.id },
    }).then(() => handleOp());
  };

  const operateAllList = [
    /*  status
            退款 1 2 3 9
            退货 1 3 4 5 6 9
            换货 1 3 4 5 6 7 8 9  
            1 售后申请待商家处理 默认状态
            2 退款审批通过 待退款
            3 退款/退货/换货审批未通过
            4 退货/换货审批通过，待买家退货/换货
            5 买家退货/换货，待商家收货
            6 商家拒绝收货，待买家处理
            7 商家收货，待商家寄出新货物
            8 商家已发货，待买家确认收货
            9 售后成功
            10 售后关闭（买家取消售后）
        */
    { text: '致电商家', handle: handleCallMerchant }, //all
    { text: '修改申请', handle: handleModifyApply }, //1 3 6
    { text: '撤销申请', handle: handleCancleApply }, //1 2 3 4 6 7
    { text: '填写单号', handle: handleLogistic }, //4
    { text: '确认收货', handle: handleConfirm }, //8
  ];

  let operatesIndex = getOperatesIndex(detail.status);

  return (
    <View className="bottomOperate flex-row-end-center">
      {operatesIndex?.length > 0 &&
        operatesIndex.map((item) => {
          return (
            <View
              className="btn flex-row-center-center"
              onClick={() => operateAllList[item].handle()}
            >
              {operateAllList[item].text}
            </View>
          );
        })}
    </View>
  );
};

export default connect(({}) => ({}))(BottomOperate);
